<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <!-- 主表单区域 -->
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="8" >
            <a-form-model-item label="名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="name">
              <a-input v-model="model.name" placeholder="请输入名称" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="8" >
            <a-form-model-item label="运维号码" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="devopsNo">
              <a-input v-model="model.devopsNo" placeholder="请输入运维号码" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="8" >
            <a-form-model-item label="运维类型" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="devopsPersontype">
              <j-dict-select-tag type="list" v-model="model.devopsPersontype" dictCode="devops_persontype" placeholder="请选择运维类型" />
            </a-form-model-item>
          </a-col>
          <a-col :span="8" >
            <a-form-model-item label="触发类型" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="triggerType">
              <a-input v-model="model.triggerType" placeholder="请输入触发类型" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="8" >
            <a-form-model-item label="重复次数" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="repeatNumber">
              <a-input v-model="model.repeatNumber" placeholder="请输入重复次数" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="8" >
            <a-form-model-item label="重复间隔" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="repeatGap">
              <a-input v-model="model.repeatGap" placeholder="请输入重复间隔" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="8" >
            <a-form-model-item label="重复间隔单位" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="repeatGapUnit">
              <a-input v-model="model.repeatGapUnit" placeholder="请输入重复间隔单位" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="8" >
            <a-form-model-item label="下次保养时间" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="maintenanceNextTime">
              <j-date placeholder="请选择下次保养时间" v-model="model.maintenanceNextTime" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="8" >
            <a-form-model-item label="创建人" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="createBy">
              <a-input v-model="model.createBy" placeholder="请输入创建人" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="8" >
            <a-form-model-item label="创建日期" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="createTime">
              <j-date placeholder="请选择创建日期" v-model="model.createTime" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="8" >
            <a-form-model-item label="保养内容" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="maintenanceContext">
              <a-textarea v-model="model.maintenanceContext" rows="4" placeholder="请输入保养内容" />
            </a-form-model-item>
          </a-col>

        </a-row>
      </a-form-model>
    </j-form-container>
      <!-- 子表单区域 -->
    <a-tabs v-model="activeKey" @change="handleChangeTabs">
      <a-tab-pane tab="运维计划历史产品明细表" :key="refKeys[0]" :forceRender="true">
        <j-vxe-table
          keep-source
          :ref="refKeys[0]"
          :loading="yksEqmDevopsProductDetailHisTable.loading"
          :columns="yksEqmDevopsProductDetailHisTable.columns"
          :dataSource="yksEqmDevopsProductDetailHisTable.dataSource"
          :maxHeight="300"
          :disabled="formDisabled"
          :rowNumber="true"
          :rowSelection="false"
          :toolbar="false"
          :edit-config="{
            showIcon: false,
          }"
          > 
          <template v-slot:serviceHistory="{ row }">
            <!-- <a @click="handleProductDetail(row, '产品详情')">{{ row.productCode }}</a> -->
            <div>
                <a-icon  @click="()=>{console.log('aaaaa')}" type="ellipsis" />
            </div>
          </template>
          </j-vxe-table>
      </a-tab-pane>
      <a-tab-pane tab="运维计划历史物料明细表" :key="refKeys[1]" :forceRender="true">
        <j-vxe-table
          keep-source
          :ref="refKeys[1]"
          :loading="yksEqmDevopsMaterialDetailHisTable.loading"
          :columns="yksEqmDevopsMaterialDetailHisTable.columns"
          :dataSource="yksEqmDevopsMaterialDetailHisTable.dataSource"
          :maxHeight="300"
          :disabled="formDisabled"
          :rowNumber="true"
          :rowSelection="false"
          :toolbar="false"
          :edit-config="{
            showIcon: false,
          }"
          />
      </a-tab-pane>
    </a-tabs>
  </a-spin>
</template>

<script>

  import { putAction,getAction } from '@/api/manage'
  import { JVxeTableModelMixin } from '@/mixins/JVxeTableModelMixin.js'
  import { JVXETypes } from '@/components/jeecg/JVxeTable'
  import { getRefPromise,VALIDATE_FAILED} from '@/components/jeecg/JVxeTable/utils/vxeUtils.js'
  import { validateDuplicateValue } from '@/utils/util'
  import JFormContainer from '@/components/jeecg/JFormContainer'

  export default {
    name: 'YksEqmDevopsServiceHisForm',
    mixins: [JVxeTableModelMixin],
    components: {
      JFormContainer,
    },
    data() {
      return {
        labelCol: {
          xs: { span: 24 },
          sm: { span: 7 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 17 },
        },
        ruleCode:'devops_no',
        model:{
          devopsNo:''
         },
        // 新增时子表默认添加几行空数据
        addDefaultRowNum: 1,
        validatorRules: {
        },
        refKeys: ['yksEqmDevopsProductDetailHis', 'yksEqmDevopsMaterialDetailHis', ],
        tableKeys:['yksEqmDevopsProductDetailHis', 'yksEqmDevopsMaterialDetailHis', ],
        activeKey: 'yksEqmDevopsProductDetailHis',
        // 运维计划历史产品明细表
        yksEqmDevopsProductDetailHisTable: {
          loading: false,
          dataSource: [],
          columns: [
             {
              title: '运维类型',
              key: 'serviceType',
              dictCode:"service_type",
              type: JVXETypes.select,
              options:[],
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
            {
              title: '产品名称',
              key: 'productName',
               type: JVXETypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
            {
              title: '位号',
              key: 'locationNumber',
               type: JVXETypes.input,
              width:"80px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
            {
              title: '运维历史',
              key: 'serviceHistory',
              type: JVXETypes.input,
              width:"100px",
              defaultValue:'',
              slots:{
                default:'serviceHistory',
              }
            },
            {
              title: '保养说明',
              key: 'remark',
               type: JVXETypes.textarea,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
            {
              title: '现场保养说明',
              key: 'serviceRemark',
               type: JVXETypes.textarea,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
            {
              title: '现场保养图片',
              key: 'attachments',
              type: JVXETypes.image,
              token:true,
              responseName:"message",
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
            {
              title: '创建日期',
              key: 'createTime',
              type: JVXETypes.date,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
            {
              title: '更换日期',
              key: 'changeDate',
              type: JVXETypes.date,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
            {
              title: '检查日期',
              key: 'checkDate',
              type: JVXETypes.date,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
            {
              title: '检查间隔(月)',
              key: 'checkGap',
               type: JVXETypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
            {
              title: '保养日期',
              key: 'maintenanceDate',
              type: JVXETypes.date,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
            {
              title: '保养间隔(月)',
              key: 'maintenanceGap',
               type: JVXETypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
            {
              title: '更换间隔(月)',
              key: 'changeGap',
               type: JVXETypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },

          ]
        },
        // 运维计划历史物料明细表
        yksEqmDevopsMaterialDetailHisTable: {
          loading: false,
          dataSource: [],
          columns: [
            {
              title: '运维类型',
              key: 'serviceType',
              type: JVXETypes.select,
              options:[],
              dictCode:"service_type",
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
            {
              title: '位号',
              key: 'locationNumber',
               type: JVXETypes.input,
              width:"80px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
             {
              title: '材料名称',
              key: 'materialName',
               type: JVXETypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
            {
              title: '现场保养说明',
              key: 'serviceRemark',
               type: JVXETypes.textarea,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
            {
              title: '保养说明',
              key: 'remark',
               type: JVXETypes.textarea,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
            {
              title: '保养照片',
              key: 'attachments',
              type: JVXETypes.image,
              token:true,
              responseName:"message",
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
            {
              title: '创建日期',
              key: 'createTime',
              type: JVXETypes.date,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
            {
              title: '更换日期',
              key: 'changeDate',
              type: JVXETypes.date,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
            {
              title: '检查日期',
              key: 'checkDate',
              type: JVXETypes.date,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
            {
              title: '检查间隔(月)',
              key: 'checkGap',
               type: JVXETypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
            {
              title: '保养日期',
              key: 'maintenanceDate',
              type: JVXETypes.date,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
            {
              title: '保养间隔(月)',
              key: 'maintenanceGap',
               type: JVXETypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
            {
              title: '更换间隔(月)',
              key: 'changeGap',
               type: JVXETypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },

          ]
        },
        url: {
          ruleBaseURL :'/sys/fillRule/executeRuleByCode/',
          add: "/srv/yksEqmDevopsServiceHis/add",
          edit: "/srv/yksEqmDevopsServiceHis/edit",
          queryById: "/srv/yksEqmDevopsServiceHis/queryById",
          yksEqmDevopsProductDetailHis: {
            list: '/srv/yksEqmDevopsServiceHis/queryYksEqmDevopsProductDetailHisByMainId'
          },
          yksEqmDevopsMaterialDetailHis: {
            list: '/srv/yksEqmDevopsServiceHis/queryYksEqmDevopsMaterialDetailHisByMainId'
          },
        }
      }
    },
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    computed: {
      formDisabled(){
        return this.disabled
      },
    },
    created () {
      this.getOrderNum()
    },
    methods: {
      getOrderNum() {
        putAction(this.url.ruleBaseURL + this.ruleCode, this.model).then(res => {
          // 执行成功，获取返回的值，并赋到页面上
          if (res.success) {
            this.model.devopsNo = res.result
            console.log(res.result)
          }
        })
      },
      addBefore(){
        this.yksEqmDevopsProductDetailHisTable.dataSource=[]
        this.yksEqmDevopsMaterialDetailHisTable.dataSource=[]
      },
      getAllTable() {
        let values = this.tableKeys.map(key => getRefPromise(this, key))
        return Promise.all(values)
      },
      /** 调用完edit()方法之后会自动调用此方法 */
      editAfter() {
        this.$nextTick(() => {
        })
        // 加载子表数据
        if (this.model.id) {
          let params = { id: this.model.id }
          this.requestSubTableData(this.url.yksEqmDevopsProductDetailHis.list, params, this.yksEqmDevopsProductDetailHisTable)
          this.requestSubTableData(this.url.yksEqmDevopsMaterialDetailHis.list, params, this.yksEqmDevopsMaterialDetailHisTable)
        }
      },
      //校验所有一对一子表表单
        validateSubForm(allValues){
            return new Promise((resolve,reject)=>{
              Promise.all([
              ]).then(() => {
                resolve(allValues)
              }).catch(e => {
                if (e.error === VALIDATE_FAILED) {
                  // 如果有未通过表单验证的子表，就自动跳转到它所在的tab
                  this.activeKey = e.index == null ? this.activeKey : this.refKeys[e.index]
                } else {
                  console.error(e)
                }
              })
            })
        },
      /** 整理成formData */
      classifyIntoFormData(allValues) {
        let main = Object.assign(this.model, allValues.formValue)
        return {
          ...main, // 展开
          yksEqmDevopsProductDetailHisList: allValues.tablesValue[0].tableData,
          yksEqmDevopsMaterialDetailHisList: allValues.tablesValue[1].tableData,
        }
      },
      validateError(msg){
        this.$message.error(msg)
      },

    }
  }
</script>

<style scoped>
</style>